package com.jeecg.p3.commonluckymoney.web.back;

import com.jeecg.p3.commonluckymoney.def.CommonluckymoneyProperties;
import com.jeecg.p3.commonluckymoney.entity.WxActCommonluckymoney;
import com.jeecg.p3.commonluckymoney.entity.WxActCommonluckymoneyAwards;
import com.jeecg.p3.commonluckymoney.exception.CommonluckymoneyException;
import com.jeecg.p3.commonluckymoney.exception.CommonluckymoneyExceptionEnum;
import com.jeecg.p3.commonluckymoney.service.WxActCommonluckymoneyAwardsService;
import com.jeecg.p3.commonluckymoney.service.WxActCommonluckymoneyService;
import com.jeecg.p3.commonluckymoney.util.ContextHolderUtils;
import org.apache.velocity.VelocityContext;
import org.jeecgframework.p3.core.common.utils.AjaxJson;
import org.jeecgframework.p3.core.util.SystemTools;
import org.jeecgframework.p3.core.util.WeiXinHttpUtil;
import org.jeecgframework.p3.core.util.plugin.ViewVelocity;
import org.jeecgframework.p3.core.utils.common.PageQuery;
import org.jeecgframework.p3.core.utils.common.StringUtils;
import org.jeecgframework.p3.core.web.BaseController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Date;
import java.util.List;

 /**
 * 描述：</b>WxActCommonluckymoneyController<br>红包活动表
 * @author pituo
 * @since：2015年12月08日 16时43分31秒 星期二 
 * @version:1.0
 */
@Controller
@RequestMapping("/commonluckymoney/back/wxActCommonluckymoney")
public class WxActCommonluckymoneyController extends BaseController{
  @Autowired
  private WxActCommonluckymoneyService wxActCommonluckymoneyService;
  @Autowired
  private WxActCommonluckymoneyAwardsService wxActCommonluckymoneyAwardsService;
  private static String domain = CommonluckymoneyProperties.domain;
/**
  * 列表页面
  * @return
  */
@RequestMapping(value="list",method = {RequestMethod.GET,RequestMethod.POST})
public void list(@ModelAttribute WxActCommonluckymoney query,HttpServletRequest request,HttpServletResponse response,
			@RequestParam(required = false, value = "pageNo", defaultValue = "1") int pageNo,
			@RequestParam(required = false, value = "pageSize", defaultValue = "10") int pageSize) throws Exception{
		VelocityContext velocityContext = new VelocityContext();
		String viewName = "commonluckymoney/back/wxActCommonluckymoney-list.vm";
		String defaultJwid = CommonluckymoneyProperties.defaultJwid;
	 	try {
		 	String jwid = ContextHolderUtils.getSession().getAttribute("jwid").toString();
		 	query.setJwid(jwid);
		 	if(defaultJwid.equals(jwid)){
				String createBy = request.getSession().getAttribute("system_userid").toString();
		 		query.setCreateBy(createBy);
		 		if(StringUtils.isEmpty(jwid)||StringUtils.isEmpty(createBy)){
			 		throw new CommonluckymoneyException(CommonluckymoneyExceptionEnum.ARGUMENT_ERROR);
			 	}
		 	}else{
		 		if(StringUtils.isEmpty(jwid)){
		 			throw new CommonluckymoneyException(CommonluckymoneyExceptionEnum.ARGUMENT_ERROR);
			 	}
		 	}
	 		PageQuery<WxActCommonluckymoney> pageQuery = new PageQuery<WxActCommonluckymoney>();
		 	pageQuery.setPageNo(pageNo);
		 	pageQuery.setPageSize(pageSize);
			pageQuery.setQuery(query);
			velocityContext.put("wxActCommonluckymoney",query);
			velocityContext.put("pageInfos",SystemTools.convertPaginatedList(wxActCommonluckymoneyService.queryPageList(pageQuery)));
		} catch (Exception e) {
			e.printStackTrace();
			// TODO: handle exception
		}
		ViewVelocity.view(request,response,viewName,velocityContext);
}

/**
 * 跳转到添加页面
 * @return
 */
@RequestMapping(value = "/toAdd",method ={RequestMethod.GET, RequestMethod.POST})
public void toAddDialog(HttpServletRequest request,HttpServletResponse response,ModelMap model)throws Exception{
	 VelocityContext velocityContext = new VelocityContext();
	 String viewName = "commonluckymoney/back/wxActCommonluckymoney-add.vm";
	 velocityContext.put("date", new Date().getTime());
	 ViewVelocity.view(request,response,viewName,velocityContext);
}

/**
 * 保存信息
 * @return
 */
@RequestMapping(value = "/doAdd",method ={RequestMethod.GET, RequestMethod.POST})
@ResponseBody
public AjaxJson doAdd(@ModelAttribute WxActCommonluckymoney wxActCommonluckymoney){
	AjaxJson j = new AjaxJson();
	try {
		wxActCommonluckymoney.setCreateTime(new Date());
		wxActCommonluckymoney.setActiveFlag("1");
		wxActCommonluckymoney.setPageView(0);
		String jwid = ContextHolderUtils.getSession().getAttribute("jwid").toString();
		String createBy = ContextHolderUtils.getSession().getAttribute("system_userid").toString();
		wxActCommonluckymoney.setCreateBy(createBy);
		wxActCommonluckymoney.setJwid(jwid);
		wxActCommonluckymoneyService.doAdd(wxActCommonluckymoney);
		j.setMsg("保存成功");
	} catch (Exception e) {
		e.printStackTrace();
		j.setSuccess(false);
		j.setMsg("保存失败");
	}
	return j;
}

/**
 * 跳转到编辑页面
 * @return
 */
@RequestMapping(value="toEdit",method = RequestMethod.GET)
public void toEdit(@RequestParam(required = true, value = "id" ) String id,HttpServletRequest request,HttpServletResponse response) throws Exception{
		 VelocityContext velocityContext = new VelocityContext();
		 String viewName = "commonluckymoney/back/wxActCommonluckymoney-edit.vm";
		 try {
			 List<WxActCommonluckymoneyAwards> wxActCommonluckymoneyAwardsList = wxActCommonluckymoneyAwardsService.queryAwardsByActId(id);
			 WxActCommonluckymoney wxActCommonluckymoney = wxActCommonluckymoneyService.queryById(id);
			 wxActCommonluckymoney.setCommonluckymoneyAwardsList(wxActCommonluckymoneyAwardsList);
			 velocityContext.put("wxActCommonluckymoney",wxActCommonluckymoney);
		 }catch (Exception e) {
			 e.printStackTrace();
		 }
		 ViewVelocity.view(request,response,viewName,velocityContext);
}

/**
 * 编辑
 * @return
 */
@RequestMapping(value = "/doEdit",method ={RequestMethod.GET, RequestMethod.POST})
@ResponseBody
public AjaxJson doEdit(@ModelAttribute WxActCommonluckymoney wxActCommonluckymoney){
	AjaxJson j = new AjaxJson();
	try {
		String msg = wxActCommonluckymoneyService.doEdit(wxActCommonluckymoney);
		if(StringUtils.isNotEmpty(msg)){
			j.setMsg(msg);
			j.setSuccess(false);
			return j;
		}else{
			j.setMsg("编辑成功");
		}
	} catch (Exception e) {
		e.printStackTrace();
		j.setSuccess(false);
		j.setMsg("编辑失败");
	}
	return j;
}


/**
 * 删除
 * @return
 */
@RequestMapping(value="doDelete",method = RequestMethod.GET)
@ResponseBody
public AjaxJson doDelete(@RequestParam(required = true, value = "id" ) String id){
		AjaxJson j = new AjaxJson();
		try {
			wxActCommonluckymoneyService.doDelete(id);
			j.setMsg("删除成功");
		} catch (Exception e) {
			e.printStackTrace();
			j.setSuccess(false);
			j.setMsg("删除失败");
		}
		return j;
}

@RequestMapping(value="getShortUrl",method = RequestMethod.POST)
@ResponseBody
public AjaxJson getShortUrl(@RequestParam(required = true, value = "id" ) String id){
	AjaxJson j=new AjaxJson();
	try {
		WxActCommonluckymoney wxActCommonluckymoney = wxActCommonluckymoneyService.queryById(id);
		String shortUrl = wxActCommonluckymoney.getShortUrl();
		if(StringUtils.isEmpty(shortUrl)){
			String hdurl=wxActCommonluckymoney.getHdurl();
			hdurl = hdurl.replace("${domain}", domain);
			shortUrl=WeiXinHttpUtil.getShortUrl(hdurl,CommonluckymoneyProperties.defaultJwid);
			if(StringUtils.isEmpty(shortUrl)){
				shortUrl=hdurl;
			}else{
				wxActCommonluckymoney.setShortUrl(shortUrl);
				wxActCommonluckymoneyService.update(wxActCommonluckymoney);
			}
		}
		if(StringUtils.isEmpty(shortUrl)){
			j.setMsg("获取地址失败！");
			j.setSuccess(false);
		}else{
			j.setObj(shortUrl);
			j.setSuccess(true);
			j.setMsg("获取地址成功！");
		}
	} catch (Exception e) {
		e.printStackTrace();
		j.setMsg("获取地址失败！");
		j.setSuccess(false);
	}
	return j;
}
@RequestMapping(value="updateCount",method = RequestMethod.POST)
@ResponseBody
public AjaxJson updateCount(@ModelAttribute WxActCommonluckymoneyAwards query,HttpServletRequest request,HttpServletResponse response){
	AjaxJson j=new AjaxJson();
	try {
		wxActCommonluckymoneyAwardsService.doEdit(query);
	} catch (Exception e) {
		e.printStackTrace();
		j.setMsg("修改失败！");
		j.setSuccess(false);
	}
	return j;
}

/**
 * 列表页面
 * @return
 */
@RequestMapping(value="relationList",method = {RequestMethod.GET,RequestMethod.POST})
public void relationList(@ModelAttribute WxActCommonluckymoneyAwards query,HttpServletRequest request,HttpServletResponse response,
		@RequestParam(required = false, value = "pageNo", defaultValue = "1") int pageNo,
		@RequestParam(required = false, value = "pageSize", defaultValue = "10") int pageSize) throws Exception{
	PageQuery<WxActCommonluckymoneyAwards> pageQuery = new PageQuery<WxActCommonluckymoneyAwards>();
 	pageQuery.setPageNo(pageNo);
 	pageQuery.setPageSize(pageSize);
 	VelocityContext velocityContext = new VelocityContext();
 	String jwid =  request.getSession().getAttribute("jwid").toString();
		query.setJwid(jwid);
	pageQuery.setQuery(query);
	velocityContext.put("WxActCommonluckymoneyAwards",query);
	velocityContext.put("pageInfos",SystemTools.convertPaginatedList(wxActCommonluckymoneyAwardsService.queryPageList(pageQuery)));
	velocityContext.put("jwid",jwid);
	String viewName = "commonluckymoney/back/wxActCommonluckymoneyPrizesSet.vm";
	ViewVelocity.view(request,response,viewName,velocityContext);
}
/**
 * @功能：进入奖品数量编辑页面
 * @param response
 * @param request
 * @param id
 * @throws Exception
 */
@RequestMapping(value="/toSetCount",method = {RequestMethod.GET,RequestMethod.POST})
public void toSetCount(HttpServletResponse response,HttpServletRequest request,
		@RequestParam String id) throws Exception{
	VelocityContext velocityContext = new VelocityContext();
	String viewName = "commonluckymoney/back/wxActCommonluckymoneyPrizesSetCount.vm";
	try {
		WxActCommonluckymoneyAwards awards = wxActCommonluckymoneyAwardsService.queryById(id);
		velocityContext.put("wxActCommonluckymoneyAwards",awards);
	} catch (Exception e) {
		e.printStackTrace();
	}
	ViewVelocity.view(request,response,viewName,velocityContext);
}

}

